package com.zs.backend.modules.documentManageent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zs.backend.common.service.OSSService;
import com.zs.backend.modules.dictionaryManagement.model.DictionaryManagement;
import com.zs.backend.modules.documentManageent.dto.DocumentManagementDTO;
import com.zs.backend.modules.documentManageent.mapper.DocumentManagementMapper;
import com.zs.backend.modules.documentManageent.model.DocumentManagement;
import com.zs.backend.common.UserHolder;
import com.zs.backend.common.utils.BeanUtil;
import com.zs.backend.common.utils.StringUtil;
import com.zs.backend.modules.documentManageent.service.DocumentManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.backend.modules.ums.model.UmsAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 文档表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2024-03-18
 */
@Service
public class DocumentManagementServiceImpl extends ServiceImpl<DocumentManagementMapper, DocumentManagement>
    implements DocumentManagementService {

    @Autowired
    private OSSService ossService;

    @Override
    public Page<DocumentManagement> list(DocumentManagementDTO dto, Integer pageSize, Integer pageNum){
            Page<DocumentManagement> page = new Page<>(pageNum,pageSize);
            QueryWrapper<DocumentManagement> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<DocumentManagement> lambda = wrapper.lambda();
            lambda.like(StringUtil.isNotEmpty(dto.getTitle()),
                DocumentManagement::getTitle, dto.getTitle());
            lambda.ge(dto.getStartCreateTime() != null,
                DocumentManagement::getCreateTime, dto.getStartCreateTime());
            lambda.le(dto.getEndCreateTime() != null,
                DocumentManagement::getCreateTime, dto.getEndCreateTime());
            lambda.orderByDesc(DocumentManagement::getUpdateTime);
            return page(page,wrapper);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean addOrUpt(DocumentManagementDTO request) {
        UmsAdmin umsAdmin = UserHolder.getCurrentAdmin();
        request.setCreateUid(umsAdmin.getId());
        request.setCreateUname(umsAdmin.getUsername());

        // 将上传的文件转化为二进制
        if(request.getDocumrntFile() != null){
            //request.setDocumentData(getDocumentData(request));

            String fileName = request.getDocumrntFile().getOriginalFilename();
            request.setDocumentDataName(fileName);
            request.setDocumentDataType(fileName.substring(fileName.lastIndexOf(".") + 1));

            String filePath = ossService.filePath(request.getDocumrntFile());
            String fileUrl = ossService.upload(request.getDocumrntFile(), filePath);
            request.setFileUrl(fileUrl);
            request.setFilePath(filePath);
        }
        DocumentManagement documentManagement = BeanUtil.beanCopyProperties(request, DocumentManagement.class);

        if(request.getId() != null){
            return updateById(documentManagement);
        }
        return save(documentManagement);
    }

    @Override
    public DocumentManagement getFileInfo(Long id) {
        QueryWrapper<DocumentManagement> wrapper = new QueryWrapper<>();
        wrapper.select("id", "document_data", "document_data_name", "document_data_type");
        wrapper.eq("id", id);
        return this.getOne(wrapper);
    }

    private byte[] getDocumentData(DocumentManagementDTO request){
        MultipartFile file = request.getDocumrntFile();
        byte[] documentData = null;
        if(file == null){
            return documentData;
        }
        try {
            documentData = file.getBytes();
            String fileName = file.getOriginalFilename();
            request.setDocumentDataName(fileName) ;
            request.setDocumentDataType(fileName.substring(fileName.lastIndexOf(".") + 1));

        }catch (Exception e){
            throw new RuntimeException("解析文件异常");
        }

        return documentData;

    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateDel(Long id) {
        DocumentManagement documentManagement = getById(id);
        // 删除文件
        ossService.deleteFile(documentManagement.getFilePath());
        return removeById(documentManagement);
    }

}
